package com.dlb.andorid.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import com.dlb.andorid.App;
import com.dlb.andorid.R;
import com.dlb.andorid.WXLoginUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;



/**
 * 微信登录页面
 *
 * @author kevin_chen 2016-12-10 下午19:03:45
 * @version v1.0
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler, WXLoginUtil.WXLoginListener {


    private IWXAPI mWeixinAPI;
    public static final String WECHAT_LOGIN_RESULT_ACTION = "com.tencent.mm.opensdk.WECHAT_LOGIN_RESULT_ACTION";
    public static final String WECHAT_LOGIN_RESULT_EXTRA = "com.tencent.mm.opensdk.WECHAT_LOGIN_RESULT_EXTRA";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxlogin);
        mWeixinAPI = WXAPIFactory.createWXAPI(this, App.WX_APP_ID, true);
        mWeixinAPI.handleIntent(this.getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mWeixinAPI.handleIntent(intent, this);//必须调用此句话

    }

    //微信发送的请求将回调到onReq方法
    @Override
    public void onReq(BaseReq req) {
        Log.e("kwan", "onReq");
    }

    //发送到微信请求的响应结果
    @Override
    public void onResp(BaseResp resp) {
        Log.e("kwan", "onResp");
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                Log.e("kwan", "ERR_OK");
                //发送成功

                if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
                    SendAuth.Resp sendResp = (SendAuth.Resp) resp;
                    String code = sendResp.code;
                    Log.e("kwan", "code:" + code);
                    getAccess_token(code);

                } else if (resp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
                   // Toast.makeText(WXEntryActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
                    finish();
                }

                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                Log.e("kwan", "ERR_USER_CANCEL");
                Toast.makeText(WXEntryActivity.this, "取消", Toast.LENGTH_SHORT).show();
                finish();
                //发送取消
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                Log.e("kwan", "ERR_AUTH_DENIED");
                Toast.makeText(WXEntryActivity.this, "ERR_AUTH_DENIED", Toast.LENGTH_SHORT).show();
                finish();
                //发送被拒绝
                break;
            default:
				Log.e("kwan", "ERR_AUTH_DENIED");
                //发送返回
                break;
        }

    }

    /**
     * 获取openid accessToken值用于后期操作
     *
     * @param code 请求码
     */
    private void getAccess_token(final String code) {
        App.mWXLoginUtil.setLoginListener(this);
        App.mWXLoginUtil.getAccess_token(code);
    }

    /**
     * 获取微信的个人信息
     *
     * @param access_token
     * @param openid
     */
    private void getUserMesg(final String access_token, final String openid) {
        String path = "https://api.weixin.qq.com/sns/userinfo?access_token="
                + access_token
                + "&openid="
                + openid;
        Log.e("kwan", "getUserMesg：" + path);

    }



    @Override
    public void onGetAccessTokenSuccess(String openid, String access_token, String unionid) {
    	Log.e("kwan", "获取到 微信unionid：" + unionid + " openid:" + openid);

        App.mUnionid = unionid;
        App.mOpenid = openid;
//        App.mJPresenter.thirdLogin1_1();


        sendPayResultBroadcast(BaseResp.ErrCode.ERR_OK);
        finish();
    }

    @Override
    public void onGetAccessTokenFailed() {

    }

    private void sendPayResultBroadcast(int resultCode) {
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
        Intent payResult = new Intent();
        payResult.setAction(WECHAT_LOGIN_RESULT_ACTION);
        payResult.putExtra(WECHAT_LOGIN_RESULT_EXTRA, resultCode);
        broadcastManager.sendBroadcast(payResult);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("kwan","WXEnter onDestroy");
    }
}
